<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
$user_id = check_cookie();
if(!check_admin("commission_arb_marq")) redirect('perso.php?action=admin');
else{
	$_SESSION['navigation'] = 'arbitrages_admin';
	$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "list";
	$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
	$table_row_id = (!empty($_REQUEST['table_row_id'])) ? $_REQUEST['table_row_id'] : 0;
	$liste_arbitres = "<option value = '' style = 'color: #C00'>---> aucun arbitre <---</option>";
	$liste_arbitres_junior = "<option value = '' style = 'color: #C00'>---> aucun arbitre <---</option>";
	$q = "select id,arbitre,arbitre_junior from membres where (arbitre = 1 or arbitre_junior = 1) and valide = 1 order by prenom,nom";
	$r = ExecRequete($q);
	while($s = LigneSuivante($r)){
		$arbitre = new membre($s->id);
		$junior = ($s->arbitre_junior) ? "[J]" : "";
		if($s->arbitre){
			$liste_arbitres .= "<option value = '$s->id'>$junior $arbitre->prenom $arbitre->nom (".$arbitre->nb_arbitrages.")</option>";
			$liste_arbitres_junior .= "<option value = '$s->id'>$junior $arbitre->prenom $arbitre->nom (".$arbitre->nb_arbitrages.")</option>";
		}
		if($s->arbitre_junior) $liste_arbitres_junior .= "<option value = '$s->id'>$junior $arbitre->prenom $arbitre->nom (".$arbitre->nb_arbitrages.")</option>";
	}
	if($action == 'form'){
		if($match_no){
			$match = new arbitrage($match_no,"no");
			$match->form_edit($table_row_id);
		}
		else{
			$match = new arbitrage("");
			$match->form_edit('',"all");
		}
	}

	elseif($action == 'update'){
		$option = (!empty($_REQUEST['option'])) ? $_REQUEST['option'] : "prochains";
		$_SESSION['affichage'] = $option;
	}
	elseif($action == 'detail'){
		$match = new arbitrage($match_no);
		$salle = new salle($match->salle);
		$date = format_date($match->date);
		$heure = format_heure($match->heure);
		$fields = describe('matchs');
		echo "<div>\n";
		echo "<h2>arbitrage $match_no</h1>";
		foreach($fields as $field => $type){
			echo leftCol("<span class = 'bold'>$field</span>");
			if($field == 'date'){
				$print = $date;
			}
			elseif($field == 'heure'){
				$print = $heure;
			}
			elseif($field == 'salle'){
				$print = $salle->nom;
			}
			elseif(strpos($field,'arbitre') !== FALSE){
				$membre = new membre($match->$field);
				$print = $membre->prenom." ".$membre->nom;
			}
			else $print = $match->$field;
			echo rightCol($print);
		}
		echo "<br class = 'clear'/>\n";
		if($match->date >= date('Y-m-d')){
			echo "<h2>autres arbitres disponibles</h2>\n";
			$bonnes = array();
			$q = "select distinct(equipe) from matchs where date = '$match->date' and equipe != '$match->equipe' and salle = '$match->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$equipe = new equipe($s->equipe,"nom");
				$bonnes[$equipe->id] = $s->equipe;
			}
			$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$match->jour' and salle = '$match->salle' and nom != '$match->equipe'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				$bonnes[$s->equipe_id] = $s->nom;
			}
			$equipes = liste('equipes');
			$del = new equipe($match->equipe,"nom");
			unset($equipes[$del->id]);
			$q = "select distinct(equipe) from matchs where date = '$match->date' and salle != '$match->salle'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if(isset($equipes)){
					foreach($equipes as $id => $nom){
						if($nom == $s->equipe){unset($equipes[$id]); }
					}
				}
			}
			if(isset($equipes)){
				$q = "select equipe_id,nom from entrainements left join equipes on equipe_id = equipes.id where jour = '$match->jour' and salle != '$match->salle' and nom != '$match->equipe'";
				$r = ExecRequete($q);
				while($s = LigneSuivante($r)){
					if(isset($equipes)){
						foreach($equipes as $id => $nom){
							if($nom == $s->nom){unset($equipes[$id]); }
						}
					}
				}
			}
			$categories = array("Bonnes disponibilit&eacute;s" => "bonnes", "Disponibles" => "equipes");
			foreach($categories as $titre => $table){
				echo "<p class = 'bold center'>$titre</p>";
				$i = 0;
				${"print_membres_".$table}= array();
				if(isset($$table)){
					foreach($$table as $id => $nom){
						$equipe = new equipe($id);
						$membres = $equipe->list_membres();
						foreach($membres as $membre_id => $membre){
							$ok = ($membre->arbitre || ($membre->arbitre_junior && strstr($match->ligue,"M16") !== FALSE)) ? 1 : 0;
							if($ok){
								foreach($membre->arbitrages as $marq_id => $marq_no){
									$arbitrage = new arbitrage($marq_no,"no");
									if($arbitrage->date == $match->date) $ok = 0;
								}
								foreach($membre->marquages as $marq_id => $marq_no){
									$marquage = new match($marq_no);
									if($marquage->date == $match->date) $ok = 0;
								}
							}
							if($ok){
								${"print_membres_".$table}[$membre->id] = $membre->prenom." ".$membre->nom." ($membre->nb_arbitrages)";
								if($membre->nb_arbitrages == "0" || $membre->nb_arbitrages == "1") ${"print_membres_".$table}[$membre->id] = "<span class = 'highlight'>".${"print_membres_".$table}[$membre->id]."</span>";
							}
						}
					}
					asort(${"print_membres_".$table});
					foreach(${"print_membres_".$table} as $membre_id => $print){
						if($table == 'bonnes' || !array_key_exists($membre_id,$print_membres_bonnes)){
							$print .=
							$echo = "<span style = 'cursor: pointer' onclick = 'show_membre($membre_id,\"$match_no\")'>$print</span>";
							if(++$i % 2) echo leftCol($echo);
							else echo rightCol($echo);
						}
					}
					if(!count(${"print_membres_".$table})) echo "<p class = 'center fade'>aucune</p>\n";
					if($i % 2) echo rightCol("");
					echo "<br class = 'clear' />";
				}
			}
		}
		else echo "<p class = 'center fade'>La date de l'arbitrage est pass&eacute;e</p>\n";
		echo "</div>\n";
	}
	elseif($action == 'confirm'){
		$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
		$match = new arbitrage($match_no,"no");
		$salle = new salle($match->salle);
		$date = format_date($match->date);
		$heure = format_heure($match->heure);
		$jour = $match->jour;
		$user_id = (!empty($_REQUEST['membre_id'])) ? $_REQUEST['membre_id'] : "";
		$field = (!empty($_REQUEST['field'])) ? $_REQUEST['field'] : "";
		if(!$user_id){
			echo "<p class = 'center bold'>Confirmer la d&eacute;sinscription &agrave; l'arbitrage du $date &agrave; $heure &agrave; $salle->nom</p>";
			echo "<p class = 'center'><button onclick = 'add_arbitrage(\"$match_no\",\"$field\",\"\",1);print_arbitrages(1);'>confirmer</button><button onclick = 'center_box(0);print_arbitrages(1)'>annuler</button></p>";
		}
		else{
			$membre = new membre($user_id);
			$matchs = $membre->matchs();
			$message = "";
			echo "<p class = 'center bold'>$membre->prenom $membre->nom</p>\n";
			foreach($membre->arbitrages as $match){
				if(format_date($match->date) == $date){
					$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un arbitrage le $date !!</p>\n";
				}
			}
			foreach($membre->marquages as $match){
				if(format_date($match->date) == $date){
					$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un marquage le $date !!</p>\n";
				}
			}
			if(!$message){
				foreach($matchs as $match){
					if(format_date($match->date) == $date){
						$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un match le $date !!</p>\n";
					}
				}
			}
			$q = "select jour, salle, heure, membre_equipes.equipe_id as equipe_id from membre_equipes left join entrainements on membre_equipes.equipe_id = entrainements.equipe_id where membre_id = '$membre->id'";
			$r = ExecRequete($q);
			while($s = LigneSuivante($r)){
				if($jour == $s->jour && $salle != $s->salle){
					$message .= "<p class = 'center bold'>!! Attention, $membre->prenom $membre->nom a un entra&icirc;nement en m&ecirc;me temps !!</p>";
				}
			}
			if($message) echo $message;
			echo "<p class = 'center bold'>Confirmer l'inscription &agrave; l'arbitrage du $date &agrave; $heure &agrave; $salle->nom</p>";
			echo "<p class = 'center'><button onclick = 'add_arbitrage(\"$match_no\",\"$field\",$user_id,1);print_arbitrages(1);'>confirmer l'inscription</button><button onclick = 'center_box(0);print_arbitrages(1)'>annuler</button></p>";
		}
	}
	elseif($action == 'add'){
		$match_no = (!empty($_REQUEST['match_no'])) ? $_REQUEST['match_no'] : "";
		$user_id = (!empty($_REQUEST['membre_id'])) ? $_REQUEST['membre_id'] : "";
		$field = (!empty($_REQUEST['field'])) ? $_REQUEST['field'] : "";
		if($user_id){
			$match = new arbitrage($match_no,"no");
			$membre = new membre($user_id);
			if($membre->arbitre || ($membre->arbitre_junior && strstr($match->ligue,"M16") !== FALSE) || $membre->fictif){
				if($field){
					$match->$field = $user_id;
					$match->update();
					mail_arbitrage($match->no);
					echo "Inscription r&eacute;ussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
				}
				else{
					if($match->arbitre1){
						$membre = new membre($match->arbitre1);
						$ddns = explode("-",$membre->ddn);
						$year = $ddns[0];
						if((date('Y') - $year >= 18) || ($membre->arbitre_junior && strstr($match->ligue,"M16") !== FALSE)|| $membre->fictif){
							$match->arbitre2 = $user_id;
							$match->update();
							mail_arbitrage($match->no);
							echo "Inscription r&eacute;ussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
						}
						else{
							$match->arbitre1 = $user_id;
							$match->update();
							mail_arbitrage($match->no);
							echo "Inscription r&eacute;ussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
						}
					}
					else{
						$match->arbitre1 = $user_id;
						$match->update();
						mail_arbitrage($match->no);
						echo "Inscription r&eacute;ussie. Un email de confirmation a &eacute;t&eacute; envoy&eacute;.";
					}
				}
			}
			else echo "$membre->prenom $membre->nom doit &ecirc;tre arbitre pour s'inscrire";
		}
		else{
			$match = new arbitrage($match_no);
			$match->$field = '';
			$match->update();
			echo "D&eacute;sinscription r&eacute;ussie.\n";
		}
	}
	elseif($action == 'print'){
		$affichage = (!empty($_SESSION['affichage'])) ? $_SESSION['affichage'] : "";
		$equipes = liste("equipes");
		$table = new table("arbitrages");
		$table->filter = 0;
		$table->order = 'date';
		$table->styleclass = '';
		$fields = array("no","ligue","jour","date","heure","salle","arbitres","info");
		foreach($fields as $field){
			$$field = new table_field($field,"arbitrages");
			if($field == 'no') $no->hidden = 1;
			elseif(strpos($field,"arbit")!== FALSE) $$field->styleclass = 'nowrap';
			elseif($field == 'info') $info->styleclass = 'nowrap';
			$table->fields[] = $$field;
		}
		$id->hidden = 1;
		$arbitres->styleclass = 'nowrap';
		$info->styleclass = 'nowrap';
		$table->onclick = "";
		$table->nb_rows = 30;
		$matchs = liste('arbitrages',"no","no");	$i = 0;
		foreach($matchs as $arbitrage){
			$match = new arbitrage($arbitrage,"no");
			if((($affichage == "prochains" && $match->date >= date('Y-m-d')) ||
				($affichage == "arbitrage" && !$match->arbitre1) ||
				($affichage == "tous"))){
				$row = new table_row();
				$key = key($table->rows) + 1;
				$row->onclick = "";
				foreach($fields as $field){
					if($field == 'salle'){
						$salle = new salle($match->$field);
						$echo = str_ireplace("salle","",$salle->nom);
					}
					elseif($field == 'info'){
						$caution = ($match->arbitre1 && ($match->arbitre2 || strpos($match->ligue,"4") !== FALSE || strpos($match->ligue,"M18") !== FALSE || strpos($match->ligue,"M16") !== FALSE || strpos($match->ligue,"M19") !== FALSE) || ereg("M2[123][FG]2",$match->ligue) !== FALSE) ? "" : "<img src = 'images/icon_warning.png' alt = '!!' height= '14' width = '14' />";
						$echo = "<a href = 'javascript:form_edit_arbitrage(\"$match->no\",1)'><img src = 'images/icon_edit.png' alt = 'i' height = '14' width = '14' style = \"cursor: pointer\" /></a><a href = 'javascript:if(confirm(\"Veuillez confirmer la suppression de l arbitrage $match->no\")) del_arbitrage(\"$match->no\",\"arbitrages\")'><img src = 'images/icon_delete.png' alt = 'supp.' height = '14' width = '14' style = \"cursor: pointer\" /></a>$caution";
					}
					elseif(strpos($field,'arbit') !== FALSE){
						$liste = (strstr($match->ligue,"M16") !== FALSE) ? $liste_arbitres_junior : $liste_arbitres;
						$echo  = "1.<select name = 'arbitre1' onchange = \"confirm_arbitrage('$match->no','arbitre1',this.options[this.selectedIndex].value,1)\" style = 'font-size: 8pt;'>";
						$echo .= str_replace("<option value = '".$match->arbitre1."'>","<option value = '".$match->arbitre1."' selected = selected>",$liste);
						$arbitre1 = new membre($match->arbitre1);
						$echo .= "</select>";
						$style = (strpos($match->ligue,"4") !== FALSE || strpos($match->ligue,"M18") !== FALSE || strpos($match->ligue,"M16")  || strpos($match->ligue,"M19") !== FALSE || ereg("M2[123][FG]2",$match->ligue) !== FALSE) ? "class = 'fade'" : "";
						$echo  .= "<br />2.<select name = 'arbitre2' $style onchange = \"confirm_arbitrage('$match->no','arbitre2',this.options[this.selectedIndex].value,1)\" style = 'font-size: 8pt;'>";
						if(strpos($match->ligue,"4") !== FALSE || strpos($match->ligue,"M18") !== FALSE || strpos($match->ligue,"M19") !== FALSE || strpos($match->ligue,"M19") !== FALSE || ereg("M2[123][FG]2",$match->ligue) !== FALSE || (strpos($match->ligue,"M16") !== FALSE && $arbitre1->arbitre)) $echo .= "<option value = '' selected = 'selected'>..-- pas n&eacute;cessaire --..</option>";
						$echo .= str_replace("<option value = '".$match->arbitre2."'>","<option value = '".$match->arbitre2."' selected = selected>",$liste);
						$echo .= "</select>";

					}
					else $echo = str_ireplace("vbc ","",$match->$field);
					$row->cols[$field] = $echo;
				}
				$table->rows[] = $row;
				$i++;
			}
		}

		$footer = "";
		echo $table->print_header();
		echo $table->print_body();
		echo $table->print_footer($footer);
	}
	else{
		entete('arbitrages',"","",1);
		$_SESSION['affichage'] = 'prochains';
		$affichages = array("prochains" => "prochains arbitrages","tous" => "tous les arbitrages", "arbitrage" => "matchs sans arbitre");

		echo "<h1>Calendrier des arbitrages</h1>";
		echo "<p class = 'center bold'>options d'affichage : ";
		echo "<select name = 'affichage' onchange = 'update_arbitrages(this.options[this.selectedIndex].value,1)'>\n";
		foreach($affichages as $option => $print){
			$selected = ($_SESSION['affichage'] == $option) ? "selected = 'selected'" : "";
			echo "<option value = '$option' $selected>$print</option>\n";
		}
		echo "</select>\n";
		echo "</p>";
		echo "<p class = 'center'><button type = 'button' onclick = 'form_new_arbitrage(\"all\")'><img src = 'images/icon_new.png' alt = '' width = '14' height = '14' />nouvel arbitrage</button></p>\n";
		echo "<div id = 'arbitrages'>\n";
		echo "</div>\n";
		echo "<script type = 'text/javascript'>print_arbitrages(1)</script>\n";
		pied();
	}

}

?>
